import axios from 'axios'
import nprogress from 'nprogress'
import qs from 'qs'


let instance = axios.create({
    baseURL: process.env.VUE_APP_BASEURL
});

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    let { method, url, data, params } = config
    let args = method === 'get' ? params : data
    if (process.env.NODE_ENV === 'development') {
        console.log(`${method}了${url}`, args ? `参数是${qs.stringify(args)}` : '');
    }
    nprogress.start()
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    nprogress.done()
    if (process.env.NODE_ENV === 'development') {
        console.log(`响应了`, response.data);
    }
    return response;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

export async function get(url, params) {
    let { data } = await instance.get(url, { params })
    return data
}

export async function post(url, params) {
    let { data } = await instance.post(url, params)
    return data
}

